﻿var wait = 60;
var lock = false;
var page = 0;
var key = "";
var searchType = "user";
var url = "";
var ReturnUrl = "";
var sortby = "";

function MessageSubmit() {
    var content = ue.getContent();
    var rid = $("#cuserId").val();
    var id = $("#userid").val();
    if (rid == "") {
        CastMsg("请先登录，再进行评论！");
        return false;
    }
    else {
        if (content == "") {
            CastMsg("无评论内容，请重新评论！")
            return false;
        }
        $.ajax({
            type: "post",
            url: "/User/MessageAdd",
            data: { content: content, rid: rid, id: id }
        }).done(function (data) {
            if (data.Statu == "ok") {
                CastMsg("评论成功");
                ue.setContent("我想说点什么...");
                if (data.Data.FromUser.Picture == null) {
                    var str = '  <div class="msg_div"><div class="height_auto"><div class="top20"><div class="reply-user"><div><a href="/User/PersonalShow/' + data.Data.FromUser.ID + '">' + data.Data.FromUser.UserName + '</a></div>';
                    str += '<div><img src="/Images/userphoto.png" class="des_img" alt="' + data.Data.FromUser.UserName + '"/></div></div>';
                    str += '<div class="reply-info"><div class="head">发表于： ' + data.Data.Time + '</div><div class="content">' + data.Data.Description + '</div></div><div class="clr"></div></div></div></div>';
                    $("#reply").prepend(str);
                    $("#wu").text("");

                } else {
                    var str = '  <div class="msg_div"><div class="height_auto"><div class="top20"><div class="reply-user"><div><a href="/User/PersonalShow/' + data.Data.FromUser.ID + '">' + data.Data.FromUser.UserName + '</a></div>';
                    str += '<div><img src="/User/ShowPicture/' + data.Data.FromUser.ID + '"  class="des_img"   alt="' + data.Data.FromUser.UserName + '" /></div></div>';
                    str += '<div class="reply-info"><div class="head">发表于： ' + data.Data.Time + '</div><div class="content">' + data.Data.Description + '</div></div><div class="clr"></div></div></div></div>';
                    console.log(str);
                    $("#reply").prepend(str);
                    $("#wu").text("");
                }
            }
            else {
                CastMsg("评论失败");
            }

        });
    }

}


//切换验证码
function changeCheckCode() {
    $("#code").attr("src", $("#code").attr("src") + 1);
}


function SearchLink(data) {
    var e = window.event;
    obj = e.target || e.srcElement;
    $(obj).parents(".search-tab").children("a").removeClass("tab_select");
    $(obj).addClass("tab_select");



    $("#resultLst").html("");
    searchType = data;
    page = 0;
    lock = false;
    Load();
}

//倒计时
function timeOut() {
    if (wait == 0) {
        $('#btnGetCode').removeAttr("disabled");
        $("#btnGetCode").val("获取验证码");
    } else {
        setTimeout(function () {
            wait--;
            $("#btnGetCode").val("获取成功" + wait + "秒后失效");
            timeOut();
        }, 1000)
    }
}

///加载产品
function LoadProducts() {
    if (lock) {
        return;
    }
    else {
        $(".more").text("正在加载~~~");
        lock = true;
        $.ajax({
            url: "/Shop/getProducts",
            type: "post",
            data: { "page": page, "type": $("#producttype").val() },
        }).done(function (data) {
            if (data.Statu == "end") {
                $(".more").html(data.Msg);
                return false;
            }
            if (data.Statu == "ok") {
                console.log(data);
                var str = "";
                for (var i = 0; i < data.Data.length; i++) {
                    str += "<div class='col-2'><img src='" + data.Data[i].Cover + "' /><p><a href='/Product/Show/" + data.Data[i].ID + "'>" + data.Data[i].ProductName + "</a></p></div>"
                }
                $("#productLst").append(str);
                $(".more").html("下拉更多~~");
            }

            if (data.Data.length == 10) {
                lock = false;
                page++;
            } else {
                $(".more").html("没有更多数据！");
            }
        });
    }
}

///加载牛人轶事
function LoadAnecdotes() {
    if (lock) {
        return;
    }
    else {
        $(".more").text("正在加载~~~");
        lock = true;
        $.ajax({
            url: "/Anecdote/GetAnecdotes",
            type: "post",
            data: { "page": page },
        }).done(function (data) {
            if (data.Statu == "end") {
                $(".more").html(data.Msg);
                return false;
            }
            if (data.Statu == "ok") {
                var str = "";
                for (var i = 0; i < data.Data.length; i++) {
                    str += "<div class='anecdote-box'><div class='anecdote-title'><a href='/Anecdote/Show/" + data.Data[i].ID + "'>" + data.Data[i].Title + "</a></div><div class='anecdote-info'><span class='date'>" + data.Data[i].Time + "</span><span class='view'>浏览次数：" + data.Data[i].Browses + "</span></div><div class='desc'>" + data.Data[i].Sumamry + "</div></div>"
                }
                $("#anecdoteLst").append(str);
                $(".more").html("下拉更多~~");
            }

            if (data.Data.length == 10) {
                lock = false;
                page++;
            } else {
                $(".more").html("没有更多数据！");
            }
        });
    }

}

///加载新闻
function LoadNews() {
    if (lock) {
        return;
    }
    else {
        $(".more").text("正在加载~~~");
        lock = true;
        $.ajax({
            url: "/News/GetNews",
            type: "post",
            data: { "page": page },
        }).done(function (data) {
            if (data.Statu == "end") {
                $(".more").html(data.Msg);
                return false;
            }
            if (data.Statu == "ok") {
                var str = "";
                for (var i = 0; i < data.Data.length; i++) {
                    str += "<div class='news-box'><div class='news-title'><a href='/News/Show/" + data.Data[i].ID + "'>" + data.Data[i].Title + "</a></div><div class='news-info'><span class='date'>" + data.Data[i].DateTime + "</span><span class='view'>浏览次数：" + data.Data[i].Browses + "</span></div><div class='desc'>" + data.Data[i].Summary + "</div></div>"
                }
                $("#newsLst").append(str);
                $(".more").html("下拉更多~~");
            }

            if (data.Data.length == 10) {
                lock = false;
                page++;
            } else {
                $(".more").html("没有更多数据！");
            }
        });
    }

}



function LoadSearchResult() {
    if (lock) {
        return;
    }
    else {
        sortby = $("#sortby").val();
        if (searchType == "user") {
            url = "/Search/SearchUsers";
        }
        else if (searchType == "group") {
            url = "/Search/SearchUserGroups";
        }
        $(".more").text("正在加载~~~");
        lock = true;
        $.ajax({
            url: url,
            type: "post",
            data: { "page": page, key: $("#key").val(), sort: sortby },
        }).done(function (data) {
            if (data.Statu == "end") {
                $(".more").html(data.Msg);
                return false;
            }
            if (data.Statu == "ok") {
                var str = "";
                for (var i = 0; i < data.Data.length; i++) {
                    str += "<div class='item'>" +
                        "<div class='img left'><a><img src='" + data.Data[i].Picture + "' /></a></div>" +
                        "<div class='left'><div class='title'><a href='" + data.Data[i].Url + "'>" + data.Data[i].Title + "</a></div><div class='desc'>" + data.Data[i].Description + "</div></div></div><div class='clr'></div>"
                }
                $("#resultLst").append(str);
                $(".more").html("下拉更多~~");
            }

            if (data.Data.length == 10) {
                lock = false;
                page++;
            } else {
                $(".more").html("没有更多数据！");
            }
        });
    }
}

function Load() {

    if ($("#productLst").length > 0) {
        LoadProducts();
    }
    if ($("#anecdoteLst").length > 0) {
        LoadAnecdotes();
    }
    if ($("#newsLst").length > 0) {
        LoadNews();
    }

    if ($("#resultLst").length > 0) {
        LoadSearchResult();
    }

}


$(document).ready(function () {
    Load();

    $("#browseshow").hide();
    $("#browsespan").mouseover(function () {
        if ($(".brshow").text() == "") {
            CastMsg("暂无浏览记录");
            return false;
        }
        $("#browseshow").fadeIn();
    });
    $("#browseshow").mouseleave(function () {
        $("#browseshow").fadeOut();
    })
    $("#clearbr").click(function () {
        if (confirm("您确定要删除浏览记录？")) {
            $.ajax({
                url: "/User/Deletebrowserecord",
                type: "get",
            }).done(function (data) {
                if (data.Statu == "ok");
                $("#browseshow").text("");
                CastMsg("暂无浏览记录");
                return false;
            });
            return false;
        }

    });



    ReturnUrl = $("#returnUrl").val();
    $(window).scroll(
     function () {
         totalheight = parseFloat($(window).height())
            + parseFloat($(window).scrollTop());
         if ($(document).height() <= totalheight) {
             Load();
         }
     });

    $(".warning").text("");

    //获取验证码
    $("#btnGetCode").click(function () {
        var code = $("#validatecode").val();
        var phone = $("#phone").val();
        var reg = /(^13\d{9}$)|(^14)[5,7]\d{8}$|(^15[0,1,2,3,5,6,7,8,9]\d{8}$)|(^17)[6,7,8]\d{8}$|(^18\d{9}$)/g;
        if (phone == "") {
            popMsg("电话号码不能为空");
            return false;
        }
        if (code == "") {
            popMsg("验证码不能为空");
            return false;
        }
        if (!(reg.test(phone))) {
            popMsg("电话号码格式不正确");
            return false;
        }
        else {
            $.ajax({
                url: "/User/SendCode",
                type: "post",
                data: { "code": code, "phone": $("#phone").val() },
            }).done(function (data) {
                if (data.Statu == "ok" || data.Statu == "OK") {
                    wait = 60;
                    $("#btnGetCode").attr("disabled", "true");
                    timeOut();
                }
                if (data.Statu == "err") {
                    $(".warning").text(data.Msg);
                }
            })
        }
    });

    //获取忘记密码验证码
    $("#btnGetForgetCode").click(function () {
        var phone = $("#phone").val();
        var reg = /(^13\d{9}$)|(^14)[5,7]\d{8}$|(^15[0,1,2,3,5,6,7,8,9]\d{8}$)|(^17)[6,7,8]\d{8}$|(^18\d{9}$)/g;
        if (phone == "") {
            popMsg("电话号码不能为空");
            return false;
        }
        if (!(reg.test(phone))) {
            popMsg("电话号码格式不正确");
            return false;
        }
        else {
            $.ajax({
                url: "/User/SendForgetCode",
                type: "post",
                data: { "phone": $("#phone").val(),"sid":$("#sid").val() },
            }).done(function (data) {
                if (data.Statu == "ok" || data.Statu == "OK") {
                    wait = 60;
                    $("#btnGetForgetCode").attr("disabled", "true");
                    timeOut();
                }
                if (data.Statu == "err") {
                    $(".warning").text(data.Msg);
                }
            })
        }
    });

    //提交密码忘记验证
    $("#btnForgetpasswordSub").click(function () {
        $(".warning").text("");
        var messagecode = $("#messagecode").val();
        var phone = $("#phone").val();
        var reg = /(^13\d{9}$)|(^14)[5,7]\d{8}$|(^15[0,1,2,3,5,6,7,8,9]\d{8}$)|(^17)[6,7,8]\d{8}$|(^18\d{9}$)/g;
        if (messagecode == "") {
            popMsg("短信验证码不能为空");
            return false;
        }
        if (phone == "") {
            popMsg("电话号不能为空");
            return false;
        }
        if (!(reg.test(phone))) {
            popMsg("电话号码格式不正确");
            return false;
        }
       
        else {
            $.ajax({
                url: "/User/PhoneFindPassword",
                type: "post",
                data: { "code": messagecode, "phone": phone ,"sid":$("#sid").val()},
            }).done(function (data) {
                if (data.Statu == "ok") {
                    window.location.href = data.BackUrl;
                }
                else if (data.Statu == "err") {
                    $(".warning").text(data.Msg);
                }
            })
        }
    });

    //提交注册
    $("#btnRegister").click(function () {
        $(".warning").text("");
        var messagecode = $("#messagecode").val();
        var phone = $("#phone").val();
        var reg = /(^13\d{9}$)|(^14)[5,7]\d{8}$|(^15[0,1,2,3,5,6,7,8,9]\d{8}$)|(^17)[6,7,8]\d{8}$|(^18\d{9}$)/g;
        if (messagecode == "") {
            popMsg("短信验证码不能为空");
            return false;
        }
        if (phone == "") {
            popMsg("电话号不能为空");
            return false;
        }
        if (!(reg.test(phone))) {
            popMsg("电话号码格式不正确");
            return false;
        }
        if (!$("#register_service").is(':checked')) {
            popMsg("请选择同意本网站服务协议");
            return false;
        }
        else {
            $.ajax({
                url: "/User/PhoneRegister",
                type: "post",
                data: { "code": messagecode, "phone": phone },
            }).done(function (data) {
                if (data.Statu == "ok") {
                    window.location.href = data.BackUrl;
                }
                else if (data.Statu == "err") {
                    $(".warning").text(data.Msg);
                }
            })
        }
    });


    // 信息完善
    $("#frmRegisterComplete").submit(function () {
        var username = $("#username").val();
        var password = $("#password").val();
        var confirm = $("#confirm").val();
        if (username == "") {
            $(".warning").text("用户名不能为空");
            return false;
        }
        if (password == "") {
            $(".warning").text("密码不能为空");
            return false;
        }
        if (confirm == "") {
            $(".warning").text("密码重复不能为空");
            return false;
        }
        if (password.length < 6) {
            $(".warning").text("密码长度不够");
            return false;
        }
        if (password.length < 6) {
            $(".warning").text("密码重复长度不够");
            return false;
        }
        if (password != confirm) {
            $(".warning").text("两次输入的密码不一致");
            return false;
        }
        $("#frmRegisterComplete").serialize().submit();
    });

    $(".filter-box a").hover(
          function () {
              $(this).addClass("seling");
          },
          function () {
              $(this).removeClass("seling");
          }
      );

    $(".fbtn").click(function () {
        var val = $(this).text();
        $(this).parents("div").children("input").val(val);
        if ($(this).attr('class').indexOf('all') < 0) {
            $(this).parents("div").children("input").val(val);
        }
        else {
            $(this).parents("div").children("input").val("");
        }
        $(this).parents("div").children("a").each(function () {
            $(this).removeClass("seled");
        });
        $(this).attr("class", "seled");

        var city = $("#city").val();
        var district = $("#district").val();
        var town = $("#town").val();
        var job = $("#job").val();
        window.location.href = "/NewRen/Index?city=" + city + "&district=" + district + "&town=" + town + "&job=" + job;
    });

    $(".pfbtn").click(function () {
        var val = $(this).text();
        $(this).parents("div").children("input").val(val);
        if ($(this).attr('class').indexOf('all') < 0) {
            $(this).parents("div").children("input").val(val);
        }
        else {
            $(this).parents("div").children("input").val("");
        }
        $(this).parents("div").children("a").each(function () {
            $(this).removeClass("seled");
        });
        $(this).attr("class", "seled");

        var city = $("#city").val();
        var industry = $("#industry").val();
        window.location.href = "/UserGroup/Index?city=" + city + "&industry=" + industry;
    });

    $(".bubtn").click(function () {
        var val = $(this).text();
        $(this).parents("div").children("input").val(val);
        if ($(this).attr('class').indexOf('all') < 0) {
            $(this).parents("div").children("input").val(val);
        }
        else {
            $(this).parents("div").children("input").val("");
        }
        $(this).parents("div").children("a").each(function () {
            $(this).removeClass("seled");
        });
        $(this).attr("class", "seled");

        var city = $("#city").val();
        var industry = $("#industry").val();
        window.location.href = "/Business/Index?city=" + city + "&industry=" + industry;
    });


    $(".sbtn").click(function () {
        var val = $(this).text();
        $(this).parents("div").children("input").val(val);
        if ($(this).attr('class').indexOf('all') < 0) {
            $(this).parents("div").children("input").val(val);
        }
        else {
            $(this).parents("div").children("input").val("");
        }
        $(this).parents("div").children("a").each(function () {
            $(this).removeClass("seled");
        });
        $(this).attr("class", "seled");

        var city = $("#producttype").val();
        page = 0;
        lock = false;
        $("#productLst").html("");
        $(".more").text();
        Load();
    });

    $(".pic-hover").mouseover(function () {
        $(this).parents(".pitem").children(".user-info-txt").slideDown(400);
    });
    $(".user-info-txt").mouseout(function () {
        $(this).slideUp(400);
    });

    $("#frmExchange").submit(function () {
        var uid = $("#userid").val();
        if (uid == "") {
            popMsg("请先登录，在兑换商品");
            return false;
        }
        $("#frmExchange").serialize().submit();
    });


    var jobMoreSlected = $(".job_items").find(".seled");
    if (jobMoreSlected.length > 0) {
        $(".job_items").show();
        $(this).parents(".items").addClass("all-items");
        $(this).parents(".fieldset").height($(this).parents(".items").height());
        $("#btnJobMore").hide();
    }
    $("#btnJobMore").click(function () {
        $(".job_items").show();
        $(this).parents(".items").addClass("all-items");
        $(this).parents(".fieldset").height($(this).parents(".items").height());
        $("#btnJobMore").hide();
    });

    var indistryMoreSlected = $(".industry_items").find(".seled");
    if (indistryMoreSlected.length > 0) {
        $(".industry_items").show();
        $(this).parents(".items").addClass("all-items");
        $(this).parents(".fieldset").height($(this).parents(".items").height());
        $("#btnIndustryMore").hide();
    }

    $("#btnIndustryMore").click(function () {
        $(".industry_items").show();
        $(this).parents(".items").addClass("all-items");
        $(this).parents(".fieldset").height($(this).parents(".items").height());
        $("#btnIndustryMore").hide();
    });

    var CityMoreSlected = $(".city_items").find(".seled");
    if (CityMoreSlected.length > 0) {
        $(".city_items").show();
        $(this).parents(".items").addClass("all-items");
        $(this).parents(".fieldset").height($(this).parents(".items").height());
        $("#btnCityMore").hide();
    }
    $("#btnCityMore").click(function () {
        $(".city_items").show();
        $(this).parents(".items").addClass("all-items");
        $(this).parents(".fieldset").height($(this).parents(".items").height());
        $("#btnCityMore").hide();
    });

    var DistrictMoreSlected = $(".district_items").find(".seled");
    if (DistrictMoreSlected.length > 0) {
        $(".district_items").show();
        $(this).parents(".items").addClass("all-items");
        $(this).parents(".fieldset").height($(this).parents(".items").height());
        $("#btnDistrictMore").hide();
    }
    $("#btnDistrictMore").click(function () {
        $(".district_items").show();
        $(this).parents(".items").addClass("all-items");
        $(this).parents(".fieldset").height($(this).parents(".items").height());
        $("#btnDistrictMore").hide();
    });

    var TownMoreSlected = $(".town_items").find(".seled");
    if (TownMoreSlected.length > 0) {
        $(".town_items").show();
        $(this).parents(".items").addClass("all-items");
        $(this).parents(".fieldset").height($(this).parents(".items").height());
        $("#btnTownMore").hide();
    }
    $("#btnTownMore").click(function () {
        $(".town_items").show();
        $(this).parents(".items").addClass("all-items");
        $(this).parents(".fieldset").height($(this).parents(".items").height());
        $("#btnTownMore").hide();
    });
});

///牛人收藏
function addUserCollect(id, sid) {
    var node = $("#hdusername");
    if (node.val() == "" || node.val() == null) {
        popMsg("请先登录！");
        console.log(ReturnUrl);
        setTimeout(function () {
            location.href = "/Login?returnUrl=" + ReturnUrl;
        }, 5000);
    }
    else {
        $.post("/User/AddCollect", { sid: sid, id: id }, function (data) {
            if (data.Statu == "ok") {
                popMsg("收藏成功！");
                window.location.reload();

            } else {
                popMsg(data.Msg);
            }
        });
    }
}
//取消收藏
function cancelUserCollect(id, sid) {
    $.post("/User/CancelCollect", { sid: sid, id: id }, function (data) {
        if (data.Statu == "ok") {
            popMsg("取消收藏成功！");
            window.location.reload();
        } else {
            popMsg(data.Msg);
        }
    });
}

///牛棚收藏
function addGroupCollect(id, sid) {
    var node = $("#hdusername");
    if (node.val() == "" || node.val() == null) {
        popMsg("请先登录！");
        setTimeout(function () {
            location.href = "/Login?returnUrl=" + ReturnUrl;
        }, 2000);
    }
    else {
        $.post("/UserGroup/AddCollect", { sid: sid, id: id }, function (data) {
            if (data.Statu == "ok") {
                popMsg("收藏成功！");
                window.location.reload();

            } else {
                popMsg(data.Msg);
            }
        });
    }
}
//取消收藏
function cancelGroupCollect(id, sid) {
    $.post("/UserGroup/CancelCollect", { sid: sid, id: id }, function (data) {
        if (data.Statu == "ok") {
            popMsg("取消收藏成功！");
            window.location.reload();
        } else {
            popMsg(data.Msg);
        }
    });
}




function like(id, sid) {
    var node = $("#hdusername");
    if (node.val() == "" || node.val() == null) {
        popMsg("请先登录！");
        return false;
    }
    $.post("/User/Up", { id: id, sid: sid }, function (data) {
        if (data.Statu == "ok") {
            popMsg(data.Msg);
            var up = $("#up").text();
            up = parseInt(up) + 1;
            $("#up").text(up);
        }
        else {
            popMsg("操作失败！请重试");
        }
    });
}

function hate(id, sid) {
    var node = $("#hdusername");
    if (node.val() == "" || node.val() == null) {
        popMsg("请先登录！");
        return false;
    }
    $.post("/User/Down", { id: id, sid: sid }, function (data) {
        if (data.Statu == "ok") {
            popMsg(data.Msg);
            var down = $("#down").text();
            down = parseInt(down) + 1;
            $("#down").text(down);
        }
        else {
            popMsg("操作失败！请重试");
        }
    });
}
function business_like(id, sid) {
    var node = $("#hdusername");
    if (node.val() == "" || node.val() == null) {
        CastMsg("请先登录！");
        return false;
    }
    $.post("/Business/Up", { id: id, sid: sid }, function (data) {
        if (data.Statu == "ok") {
            popMsg(data.Msg);
            var up = $("#business_up").text();
            up = parseInt(up) + 1;
            $("#business_up").text(up);
        }
        else {
            popMsg("操作失败！请重试");
        }
    });
}

function business_hate(id, sid) {
    var node = $("#hdusername");
    if (node.val() == "" || node.val() == null) {
        CastMsg("请先登录！");
        return false;
    }
    $.post("/Business/Down", { id: id, sid: sid }, function (data) {
        if (data.Statu == "ok") {
            popMsg(data.Msg);
            var down = $("#business_down").text();
            down = data.Data;
            $("#business_down").text(down);
        }
        else {
            popMsg("操作失败！请重试");
        }
    });
}

function group_like(id, sid) {
    var node = $("#hdusername");
    if (node.val() == "" || node.val() == null) {
        CastMsg("请先登录！");
        return false;
    }
    $.post("/UserGroup/Up", { id: id, sid: sid }, function (data) {
        if (data.Statu == "ok") {
            popMsg(data.Msg);
            var up = $("#group_up").text();
            up = data.Data;
            $("#group_up").text(up);
        }
        else {
            popMsg("操作失败！请重试");
        }
    });
}

function group_hate(id, sid) {
    var node = $("#hdusername");
    if (node.val() == "" || node.val() == null) {
        CastMsg("请先登录！");
        return false;
    }
    $.post("/UserGroup/Down", { id: id, sid: sid }, function (data) {
        if (data.Statu == "ok") {
            popMsg(data.Msg);
            var down = $("#group_down").text();
            down = parseInt(down) + 1;
            $("#group_down").text(down);
        }
        else {
            popMsg("操作失败！请重试");
        }
    });
}

function changetab(n,t,body) {
    $(body).height($("#" + t).height()+36);
    $(n).hasClass("on") || ($(n).addClass("on"), $(n).siblings("li").removeClass("on"), $("#" + t).show(), $("#" + t).siblings(".infoCon").hide())
}